import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

from Base.BasePage import BasePage
from Tools.tools import DriverTools


class PageRegister(BasePage):
    def __init__(self,driver):
        # 手机元素定位
        super().__init__(driver)
        # 手机号元素定位
        self.phone = (By.ID, "phone")
        # 密码元素定位
        self.password = (By.ID, "password")
        # 验证码元素定位
        self.code = (By.ID, "verifycode")
        # 短信验证码
        self.sms_code = (By.ID, "phone_code")
        # 短信验证码按钮
        self.sms_code_button = (By.ID, "get_phone_code")
        # 注册按钮
        self.register_button = (By.CLASS_NAME, "lg-btn")
        self.result_ok = (By.XPATH, '//*[@id="step3"]/div/div/h1')
        self.error = (By.XPATH, '//*[@id="reg_form"]/div[1]')

    def register(self, phone, password, code, sms_code="666666"):
        # 填写手机号
        self.get_element(self.phone).send_keys(phone)
        # 填写密码
        self.get_element(self.password).send_keys(password)
        # 填写验证码
        self.get_element(self.code).send_keys(code)
        # 点击获取短信验证码
        self.get_element(self.sms_code_button).click()
        time.sleep(3)
        # 填写短信验证码
        self.get_element(self.sms_code).send_keys(sms_code)
        # 点击登录按钮
        self.get_element(self.register_button).click()
        time.sleep(3)

    # 断言
    def assert_register_success(self):
        # 获取注册成功页面的文本
        time.sleep(3)
        result = self.get_element(self.result_ok).text
        # 断言
        return result

    def assert_register_fail(self):
        # 获取注册失败页面的文本
        # time.sleep(3)
        result2 = self.get_element(self.error).text
        # 断言
        return result2


if __name__ == '__main__':
    driver = DriverTools.get_driver()
    driver.get("http://121.43.169.97:8081/common/member/reg")
    reg = PageRegister(driver)
    reg.register()
